/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
/**
 * @class Ext.ux.TabCloseMenu
 * @extends Object
 * Plugin (ptype = 'tabclosemenu') for adding a close context menu to tabs. Note that the menu respects
 * the closable configuration on the tab. As such, commands like remove others and remove all will not
 * remove items that are not closable.
 *
 * @constructor
 * @param {Object} config The configuration options
 * @ptype tabclosemenu
 */
Ext.supreme.SearchResultMenu = Ext.extend(Ext.menu.Menu, {
    downloadFileText: 'Download file',
    downloadSelectedFilesText: 'Download selected files',
    showHostInfoText: 'Show host informations',

    constructor : function(config){
        Ext.apply(this, config || {});
    },

    //public
    init : function(grid){
        this.grid = grid;
        grid.on({
            scope: this,
            cellcontextmenu: this.onContextMenu,
            celldblclick: this.onDbClick,
            destroy: this.destroy
        });
    },

    destroy : function(){
        Ext.destroy(this.menu);
        delete this.menu;
        delete this.grid;
        delete this.active;
    },

    onContextMenu : function(grid, rowIndex, cellIndex, e){
        this.url = this.getUrl(grid, rowIndex);
        this.host = this.getHost(grid, rowIndex);
        var m = this.createMenu();
        e.stopEvent();
        m.showAt(e.getPoint());
    },

    getUrl: function(grid, rowIndex){
        var record = grid.getStore().getAt(rowIndex);
        return record.get('url');
    },

    getHost: function(grid, rowIndex){
        var record = grid.getStore().getAt(rowIndex);
        return record.get('host');
    },

    createMenu : function(){
        if(!this.menu){
            var items = [{
                  itemId: 'downloadFile',
		  text: this.downloadFileText,
		  scope: this,
		  href: this.url
              },{
                  itemId: 'downloadSelectedFiles',
		  text: this.downloadSelectedFilesText,
		  scope: this,
		  handler: this.downloadSelectedFiles
                  //href: this.url
              },{
                  itemId: 'showHostInfo',
		  text: this.showHostInfoText,
		  scope: this,
		  handler: this.showHostInfo
                  //href: this.url
              }
            ]
            this.menu = new Ext.menu.Menu({
                items: items
            });
        }
        return this.menu;
    },
    
    onDbClick : function(grid, rowIndex, cellIndex, e){
        this.url = this.getUrl(grid, rowIndex);
        this.downloadFile();
    },

    downloadSelectedFiles: function(){

        //window.open(this.url, 'Pobieranie pliku');
    },

    showHostInfo: function(){
        var win = new Ext.supreme.HostInfoWindow({
            host:this.host,
            titlePrefix: 'Informacje o '
        });
        win.show(this);
    }

});

Ext.preg('searchresultmenu', Ext.supreme.SearchResultMenu);